# -*- coding: UTF-8 -*-
#import os
import tkinter.messagebox
import tkinter as tk

from business.dealmatrix import delMatrix
from business.inputcheck import *
from business.runtimeError import runtimeError

# 初始化日志
log = Logger("appUI.py").logger
# 将要改变的量设成全局变量
window = tk.Tk()
v = tk.StringVar()
theEntry = tk.Entry(window, bd=3, width=5)
theText = tk.Text(window, bd=3, height=10, width=57)
theLabel4 = tk.Label(window, textvariable=v)


def button1Action():
    try:
        # 关于tkinter中text的get方法：
        # 第一个参数'0.0'是指从第0行第0列开始读取('1.3'表示从第一行第3列开始读取)
        # 第二个参数End表示最后一个字符
        if theEntry.get() == '' or theText.get("0.0", "end") == '\n':
            log.error("输入值为空！")
            raise runtimeError("输入值为空！")
        else:
            num_matrix = int(theEntry.get())
            matrix_str = theText.get("0.0", "end")
            # 检查矩阵行数
            num_out_index(num_matrix)
            # 检查矩阵元素合法性
            arg_element = ele_check(matrix_str)
            str_illegal(num_matrix, arg_element)
            # 开始计算闭包
            trans_dict = delMatrix(num_matrix, arg_element)
            v.set("闭包次数:"+str(trans_dict.get('closure_times'))+"\n闭包矩阵:\n"+str(trans_dict.get('closure_Matrix')))
    except runtimeError as e:
        print(e)
        tk.messagebox.showerror(title='运行错误', message=e.str_explanation)


def button2Action():
    pass


def AppUI():
    window.title("传递闭包计算器")
    # geometry参数含义 :height*width+x+y
    window.geometry("450x450+450+120")
    # window不可改变大小
    window.resizable(width=False, height=False)
    #window.iconbitmap(os.getcwd()+"/Calculator.ico")
    v.set("未运行")
    theLabel1 = tk.Label(window, text="输入模糊矩阵行列数:")
    theLabel2 = tk.Label(window, text="输入模糊矩阵元素数(默认英文'.'或空格为元素分割符):")
    theLabel3 = tk.Label(window, text="运行结果:")
    theButton1 = tk.Button(window, text="计算传递闭包次数", bd=3, width=15, command=button1Action)
    theButton2 = tk.Button(window, text="对闭包分类", bd=3, width=15, command=button2Action)
    theLabel1.place(x=20, y=30)
    theEntry.place(x=140, y=27)
    theLabel2.place(x=20, y=60)
    theText.place(x=20, y=85)
    theButton1.place(x=60, y=230)
    theButton2.place(x=260, y=230)
    theLabel3.place(x=20, y=270)
    theLabel4.place(x=20, y=290)
    window.mainloop()
